<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/31
 * Time: 11:04
 */

namespace App\Utils;


use Illuminate\Support\Str;

class StringUtils
{

    public static function to_camel($string)
    {
        return Str::camel($string);
    }

    public static function lpad($string, $chr, $len)
    {
        return self::pad($string, $chr, $len, true);
    }

    public static function rpad($string, $chr, $len)
    {
        return self::pad($string, $chr, $len, false);
    }

    public static function pad($string, $chr, $len, $left = true)
    {
        while (true) {
            if (strlen($string) >= $len) {
                break;
            }
            if ($left) {
                $string = $chr . $string;
            } else {
                $string = $string . $chr;
            }
        }
        return $string;
    }

    static public function explodeByLen($string, $len)
    {
        $matrix = [];
        $temp = $string;
        while (true) {
            $size = mb_strlen($temp);
            if ($size > $len) {
                $s = mb_substr($temp, 0, $len);
                array_push($matrix, $s);
                $temp = mb_substr($temp, $len);
            } else {
                array_push($matrix, $temp);
                break;
            }
        }
        return $matrix;
    }

    static public function fetchSamePrefix($string1, $string2)
    {
        $index = 0;
        $size1 = mb_strlen($string1);
        $size2 = mb_strlen($string2);
        $len = min($size1, $size2);
        for ($i = 0; $i < $len; $i++) {
            $temp1 = mb_substr($string1, $i, 1);
            $temp2 = mb_substr($string2, $i, 1);
            if ($temp1 != $temp2) {
                break;
            }
            $index = $i + 1;
        }
        if ($index == 0) {
            return "";
        }
        return mb_substr($string1, 0, $index);
    }

}